#include "capturethread.h"

CaptureThread::CaptureThread(QCamera* camera): doRun(true)
{
    // 创建摄像头图片抓取
    cameraCapture = new QCameraImageCapture(camera);
    QImageEncoderSettings imageSettings;
    imageSettings.setCodec("image/jpeg");
    imageSettings.setResolution(250, 250);
    cameraCapture->setEncodingSettings(imageSettings);
    // 抓拍模式为缓存模式
    cameraCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
    // 抓拍事项处理
    connect(cameraCapture, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
        qDebug() << "Captured image:" << id;
        emit capture(id, img);
    });
}

void CaptureThread::SetRun(bool flag)
{
    this->doRun = flag;
}

void CaptureThread::run()
{
    while(doRun) {
        // 对象创建失败
        if (NULL == cameraCapture) {
            break;
        }
        // 定期抓拍图片
        cameraCapture->capture();
        // 每200毫秒抓一次
        this->msleep(200);
    }
    if (cameraCapture != NULL) {
        delete cameraCapture;
        cameraCapture = NULL;
    }
}
